package com.ruoyi.product.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.market.constant.RedisKeyConstent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.ProductProfitMapper;
import com.ruoyi.db.domain.ProductProfit;
import com.ruoyi.product.service.IProductProfitService;
import com.ruoyi.common.core.text.Convert;

/**
 * profitService业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-31
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ProductProfitServiceImpl implements IProductProfitService 
{
    @Autowired
    private ProductProfitMapper productProfitMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询profit
     * 
     * @param id profit主键
     * @return profit
     */
    @Override
    public ProductProfit selectProductProfitById(String id)
    {
        return productProfitMapper.selectProductProfitById(id);
    }

    /**
     * 查询profit列表
     * 
     * @param productProfit profit
     * @return profit
     */
    @Override
    public List<ProductProfit> selectProductProfitList(ProductProfit productProfit)
    {
        return productProfitMapper.selectProductProfitList(productProfit);
    }

    /**
     * 查询profit列表
     *
     * @param productProfit profit
     * @return profit
     */
    @Override
    public List<Map<String, Object>> selectProductProfitListMap(ProductProfit productProfit)
    {
        return productProfitMapper.selectProductProfitListMap(productProfit);
    }

    /**
     * 新增profit
     * 
     * @param productProfit profit
     * @return 结果
     */
    @Override
    public int insertProductProfit(ProductProfit productProfit)
    {
        productProfit.setCreateTime(DateUtils.getNowDate());
        return productProfitMapper.insertProductProfit(productProfit);
    }

    /**
     * 修改profit
     * 
     * @param productProfit profit
     * @return 结果
     */
    @Override
    public int updateProductProfit(ProductProfit productProfit)
    {
        productProfit.setUpdateTime(DateUtils.getNowDate());
        String nowDate = LocalDate.now().toString();
        int i = productProfitMapper.updateProductProfit(productProfit);
        String redisKey = StrUtil.format(RedisKeyConstent.PRODUCT_DAY_UV_COUNT_KEY, nowDate, productProfit.getProductId());
        if (i>0){
            redisTemplate.delete(redisKey);
        }
        return i;
    }

    /**
     * 批量删除profit
     * 
     * @param ids 需要删除的profit主键
     * @return 结果
     */
    @Override
    public int deleteProductProfitByIds(String ids)
    {
        return productProfitMapper.deleteProductProfitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除profit信息
     * 
     * @param id profit主键
     * @return 结果
     */
    @Override
    public int deleteProductProfitById(String id)
    {
        return productProfitMapper.deleteProductProfitById(id);
    }
}
